<template>
    <div style="height: 100%">
            <bpmn-designer v-model="info" @save="onSave"/>
    </div>
</template>
<script>
import { info, saveModelEditor } from '@/api/model.js'

export default {
  name: 'ModelEditor',
  data() {
    return {
      info: {
        modelId: this.$route.query.id,
        xml: '',
        processId: '',
        processName: ''
      }
    }
  },
  mounted() {
    this.loadInfo()
  },
  methods: {
    loadInfo() {
      if (this.info.modelId) {
        info({ id: this.info.modelId }).then(({ data }) => {
          if (data) {
            this.info.processId = data.id
            this.info.processName = data.name
            this.info.xml = data?.extra?.xml ?? ''
          }
        })
      }
    },
    onSave(args) {
      if (this.info.modelId) {
        saveModelEditor({ id: this.info.modelId, json: args.json, xml: args.xml, svg: args.svg }).then(res => {
          if (res.success) {
            this.$message.success('保存成功')
            window.history.go(-1)
          } else {
            this.$message.success('保存失败')
          }
        })
      }
    }
  }
}
</script>
